<?php


namespace App\Http\Controllers\Admin;


use App\DTOS\Admin\HorseRaceLampDTO;
use App\Http\Controllers\Controller;
use App\Models\Game\HorseRaceLamp;
use App\Services\Game\HorseRaceLampService;
use App\Utils\ResponseUtil;
use Illuminate\Support\Facades\DB;

// HorseRaceLamp
class HorseRaceLampController extends Controller
{
    private HorseRaceLampService $HorseRaceLampService;

    /**
     * HorseRaceLampController constructor.
     * @param HorseRaceLampService $HorseRaceLampService
     */
    public function __construct(HorseRaceLampService $HorseRaceLampService)
    {
        $this->HorseRaceLampService = $HorseRaceLampService;
    }

    public function add()
    {
        $input = request()->all();
        $intervalTime = (int)data_get($input,'intervalTime');
        $codes = array_values(data_get($input,'game_area',[]));
        $type = data_get($input,'type');
        if ($codes) $codes = strArrToIntArr($codes);
        if ($intervalTime % 5 !== 0) {
            return ResponseUtil::error("推送间隔时间必须是5的倍数");
        }
        $data = [];
        $data['title'] = data_get($input,'title');
        $data['content'] = data_get($input,'content');
        $data['begin_time'] = strtotime($input['beginTime']);
        $data['end_time'] = strtotime($input['endTime']);
        $data['type'] = $type;
        $data['confirm'] = 0;
        if ($type == 0){
            $data['game_areas'] = json_encode($codes);
        } else {
            $data['game_areas'] = json_encode([]);
        }
        $data['interval_time'] = $intervalTime;
        $data['updated_at'] = $data['created_at'] = date("Y-m-d H:i:s");
        HorseRaceLamp::query()->insert($data);
        return ResponseUtil::success();
    }

    public function paginate()
    {
        $dto = new HorseRaceLampDTO(request()->all());
        $paginate = $this->HorseRaceLampService->paginate($dto);
        $data = $paginate->items();
        foreach($data as &$datum){
            $datum['status'] = ($datum['begin_time'] < time() && $datum['end_time'] > time()) ? 1 : -1; // 1表示正常, -1表示失效
            $datum['beginDateTime'] = date("Y-m-d H:i:s",$datum['begin_time']);
            $datum['endDateTime'] = date("Y-m-d H:i:s",$datum['end_time']);
        }
        $res = [];
        $res['code'] = 0;
        $res['data'] = $data;
        return ResponseUtil::json($res);
    }
    public function confirm($id){
        $row = HorseRaceLamp::query()->find($id);
        $row->confirm = 1;
        $row->save();
        return ResponseUtil::success();
    }
    public function delete($id)
    {
        $detail = HorseRaceLamp::query()->find($id);
        $status = ($detail['begin_time'] < time() && $detail['end_time'] > time()) ? 1 : -1; // 1表示正常, -1表示失效
        if ($status===1){
            return ResponseUtil::error("跑马灯生效中无法删除");
        }
        $detail->delete();
        return ResponseUtil::success();
    }
    public function detail($id)
    {
        $detail = HorseRaceLamp::query()->find($id);
        return ResponseUtil::success($detail);
    }
    public function edit($id)
    {
        DB::beginTransaction();
        try {
            $input = request()->all();
            $intervalTime = (int)data_get($input,'intervalTime');
            $codes = array_values(data_get($input,'game_area',[]));
            $type = data_get($input,'type');
            if ($codes) $codes = strArrToIntArr($codes);
            if ($intervalTime % 5 !== 0) {
                return ResponseUtil::error("推送间隔时间必须是5的倍数");
            }
            $data = [];
            $data['title'] = data_get($input,'title');
            $data['content'] = data_get($input,'content');
            $data['begin_time'] = strtotime($input['beginTime']);
            $data['end_time'] = strtotime($input['endTime']);
            $data['type'] = $type;
            $data['confirm'] = 0;
            if ($type == 0){
                $data['game_areas'] = json_encode($codes);
            } else {
                $data['game_areas'] = json_encode([]);
            }
            $data['interval_time'] = $intervalTime;
            $data['execution_time'] = 0;
            $data['updated_at'] = date("Y-m-d H:i:s");
            HorseRaceLamp::query()->where('id',$id)->update($data);
            DB::commit();
            return ResponseUtil::success();
        }catch(\Exception $e){
            DB::rollBack();
            return ResponseUtil::error($e->getMessage());
        }
    }

}
